package com.apes.fn.business.fixedLoss.modle;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: apes-server
 * @description: 原到账明细更名为回款
 * @author: czs
 * @create: 2022-08-02 14:06
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LossReceivedItem.CONST_TABLE_NAME)
public class LossReceivedItem extends Model {
    public final static String CONST_TABLE_NAME = "ser_loss_received_item";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "定损回款明细编码", padding = 4)
    @Comment("ID")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "loss_Quotation", referencedColumnName = "id")
    @Comment("定损报价单号")
    private LossQuotation lossQuotation;

    @Column(name = "source_from", length = 16)
    @Comment("来源单号")
    private String sourceFrom;

    @Column(name = "source_business_type", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "received_date")
    @Comment("到账时间")
    private Date receivedDate;

    @States(name = "单据状态", values = {
            @State(id = "none", name = "未到账"),
            @State(id = "portion", name = "部分到账"),
            @State(id = "all", name = "全部到账"),
    })
    @Column(name = "received_state")
    @Comment("到账状态")
    private String receivedState = "none";

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account_type", referencedColumnName = "id")
    @Comment("账户类型")
    private Enumeration accountType ;

    @Column(name = "serial_number", length = 64)
    @Comment("流水号")
    private String serialNumber;

    @Column(name = "received_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("到账金额")
    private double receivedAmount = 0;


    @Column(name = "apportion_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总分摊金额")
    private double apportionAmount = 0;

    public void setApportionAmount(double apportionAmount) {
        this.apportionAmount = apportionAmount;
    }

    public void addApportionAmount(double apportionAmount) {
        this.apportionAmount += apportionAmount;
    }

    @Column(name = "remark", length = 256)
    @Comment("备注")
    private String remark;

    @Column(name = "operator", length = 64)
    @Comment("操作人")
    private String operator;

//    @OneToMany(mappedBy = "lossQuotation", cascade = CascadeType.ALL, orphanRemoval = true)
//    private List<LossReceivedApportion> lossReceivedApportion = new ArrayList();
//
//    public List<LossReceivedApportion> getLossReceivedApportion() {
//        if(lossReceivedApportion.size() == 0) {
//            return lossReceivedApportion;
//        }
//        for (LossReceivedApportion item : lossReceivedApportion) {
//
//            item.setLossReceivedItem(this);
//        }
//        return lossReceivedApportion;
//    }

    @Column(name = "to_prepare_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("缴款金额")
    private double toPrepareAmount = 0;

    public void setToPrepareAmount(double toPrepareAmount) {
        this.toPrepareAmount = toPrepareAmount;
    }

    public void addToPrepareAmount(double toPrepareAmount) {
        this.toPrepareAmount += toPrepareAmount;
    }

    @Column(name = "apportion_complete", columnDefinition = "number(1,0) default 0")
    private boolean apportionComplete = false;

}
